package com.iflytek.crvoice2txt.activity;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Typeface;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.iflytek.crvoice2txt.R;
import com.iflytek.crvoice2txt.adapter.SignItemAdapter;
import com.iflytek.crvoice2txt.entity.RecorderEntity;
import com.iflytek.crvoice2txt.present.RecoderSignPresent;
import com.iflytek.crvoice2txt.present.RecoderSignPresentImpl;
import com.iflytek.crvoice2txt.util.NotificationUtil;

import java.util.ArrayList;
import java.util.List;

import cn.pedant.SweetAlert.SweetAlertDialog;

public class RecoderSignActivity extends BaseActivity implements RecordSignView {

    private TextView tv_switch_speak;
    private boolean isSwitchPressed = false;
    private RecoderSignPresent recoderSignPresent;
    private LinearLayout ll_top_bar_back;
    private RelativeLayout rl_recoder_sign_speaking;
    private TextView tv_recoder_sign_speaking, tv_save, tv_topbar_title;
    private ListView lv_recoder_sign;
    private SignItemAdapter signItemAdapter;
    private List<RecorderEntity> list;
    private NotificationUtil notificationUtil;
    private SweetAlertDialog sweetAlertDialog;

    @Override
    protected void init() {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},1);
        }
        notificationUtil = new NotificationUtil(this);
        recoderSignPresent = new RecoderSignPresentImpl(this, this);
        list = new ArrayList<>();
    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_recoder_sign;
    }

    @Override
    public void initView(View view) {

        lv_recoder_sign = findViewById(R.id.lv_recoder_sign);
        tv_save = findViewById(R.id.tv_save);
        tv_switch_speak = findViewById(R.id.tv_switch_speak);
        ll_top_bar_back = findViewById(R.id.ll_top_bar_back);
        ll_top_bar_back.setVisibility(View.VISIBLE);
        rl_recoder_sign_speaking = findViewById(R.id.rl_recoder_sign_speaking);
        tv_recoder_sign_speaking = findViewById(R.id.tv_recoder_sign_speaking);
        signItemAdapter = new SignItemAdapter(list, this);
        lv_recoder_sign.setAdapter(signItemAdapter);
    }

    @Override
    public void setListener() {

        tv_switch_speak.setOnClickListener(this);
        ll_top_bar_back.setOnClickListener(this);
        signItemAdapter.setOnSaveBtnClickListener(new SignItemAdapter.OnItemClickListener() {
            @Override
            public void onSaveBtnClick(int position, String str) {
                //recoderSignPresent.stopVoiceDictation();
                isSwitchPressed = false;
                tv_switch_speak.setBackgroundColor(getResources().getColor(R.color.A17));
                tv_switch_speak.setText(getResources().getString(R.string.start_speak));
                rl_recoder_sign_speaking.setVisibility(View.GONE);
                recoderSignPresent.saveText(list.get(position));
                list.get(position).isCurrent = false;
                signItemAdapter.notifyDataSetChanged();
                lv_recoder_sign.smoothScrollToPosition(position);
            }

            @Override
            public void onStartPlayAudio(int position) {
                //recoderSignPresent.startPlayAudio(list.get(position).audio_path);
            }

            @Override
            public void onStopPlayAudio() {
                //recoderSignPresent.stopPlayAudio();
            }

            @Override
            public void changeList(String text, int position) {
                if (list != null) {
                    if (list.get(position) != null) {
                        list.get(position).text = text;
                    }
                }
            }

            @Override
            public void onEditTextDataChanged(int position, String text) {
                list.get(position).text = text;
            }
        });
    }

    @Override
    public void doBusiness(Context mContext) {
        recoderSignPresent.getSaveList();
    }

    @Override
    public void doErrroInit(int errorcode) {
    }

    @Override
    public void showToast(String str) {
        Toast.makeText(RecoderSignActivity.this, str, Toast.LENGTH_SHORT).show();
    }

    @Override
    public String getEtText() {
        return "";
    }

    @Override
    public void dicationFinished(RecorderEntity recorderEntity) {
        Log.i("RecordSignBizImpl1", recorderEntity.toString());
        tv_switch_speak.setBackgroundColor(getResources().getColor(R.color.A17));
        tv_switch_speak.setText(getResources().getString(R.string.start_speak));
        rl_recoder_sign_speaking.setVisibility(View.GONE);
        isSwitchPressed = false;
        if (recorderEntity != null && recorderEntity.text != null && !"".equals(recorderEntity.text)) {
            list.add(recorderEntity);
            refreshListView();
            Log.i("RecordSignBizImpl", list.get(list.size() - 1).toString());
        }else{
            showToast("没有听清您在说什么");
        }
    }

    @Override
    public void saveTextToDbFinished(Boolean b) {
        String str = b ? "保存成功！" : "保存失败!";
        showToast(str);
    }

    /**
     * 用户说话倒计时
     *
     * @param sed
     */
    @Override
    public void countDown(int sed) {
        tv_recoder_sign_speaking.setText("您还可以说" + sed + "秒");
    }

    @Override
    public void onQueryAllAsyn(List<RecorderEntity> list) {
        this.list.clear();
        for (RecorderEntity recorderEntity : list) {
            recorderEntity.isCurrent = false;
            this.list.add(recorderEntity);
            if(this.list.size()>5){
                this.list.remove(0);
            }
        }
        signItemAdapter.notifyDataSetChanged();
    }

    @Override
    public void onAudioPlayFinished() {

    }

    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {

            case R.id.tv_switch_speak:
                if (!isSwitchPressed) {
                    isSwitchPressed = true;
                    tv_switch_speak.setBackgroundColor(getResources().getColor(R.color.A18));
                    tv_switch_speak.setText(getResources().getString(R.string.end_speak));
                    rl_recoder_sign_speaking.setVisibility(View.VISIBLE);
                    recoderSignPresent.startVoiceDictation();
                } else {
                    isSwitchPressed = false;
                    tv_switch_speak.setBackgroundColor(getResources().getColor(R.color.A17));
                    tv_switch_speak.setText(getResources().getString(R.string.start_speak));
                    rl_recoder_sign_speaking.setVisibility(View.GONE);
                    recoderSignPresent.stopVoiceDictation();
                }
                break;
            case R.id.ll_top_bar_back:
                finish();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        recoderSignPresent.onDestroy();
    }

    @Override
    protected void onResume() {
        recoderSignPresent.onResume();
        super.onResume();
    }

    @Override
    protected void onPause() {
        recoderSignPresent.onPause();
        super.onPause();
    }

    private void refreshListView() {
        if (list == null || list.size() == 0) {
            return;
        }
        for (int i = 0; i < list.size(); i++) {
            list.get(i).isCurrent = i == list.size() - 1 ? true : false;
            /*if (list.get(i).text == null || list.get(i).text.equals("")) {
                list.remove(list.get(i));
            }*/
        }
        signItemAdapter.notifyDataSetChanged();
        lv_recoder_sign.smoothScrollToPosition(list.size() - 1);
    }

}
